﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;
using System.Drawing;

namespace BSCMFF.Web.UserControls.Details
{
    public partial class EvaluationDetails : System.Web.UI.UserControl
    {
        private Evaluation_Period _eval;// = new Evaluation();

        public Evaluation_Period Evalut
        {
            get
            {
                if (_eval == null)
                {
                    _eval = new Evaluation_Period();
                    int tempID = 0;
                    Int32.TryParse(HiddenFieldID.Value, out tempID);
                    _eval.ID = tempID;
                    _eval.Name = RadTextBoxName.Text;
                    _eval.Description = RadTextBoxDescription.Text;
                    if (RadDatePickerStartDate.SelectedDate != null)
                    {
                        _eval.StartDate = (DateTime)RadDatePickerStartDate.SelectedDate;
                    }
                    if (RadDatePickerEndDate.SelectedDate != null)
                    {
                        _eval.EndDate = (DateTime)RadDatePickerEndDate.SelectedDate;
                    }
                    _eval.Repetition = CheckBoxRepetition.Checked;
                    return _eval;
                }
                else
                {
                    return _eval;
                }
            }
            set
            {
                _eval = value;
            }
        }

        public bool IsEditMode { get; set; }


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsEditMode) //preview mode
            {
                foreach (Control c in this.Controls)
                {
                    if (c.GetType().Name == "RadTextBox")
                    {
                        c.Visible=false;
                        Label l = (Label)this.FindControl(c.ID.Replace("RadTextBox", "LabelCurrent"));
                        l.Visible=true;
                    }
                    if (c.GetType().Name == "RadDatePicker")
                    {
                        c.Visible=false;
                        //RadDatePicker
                        Label l = (Label)this.FindControl(c.ID.Replace("RadDatePicker", "LabelCurrent"));
                        if (l.Text != "")
                        {
                            l.Text = DateTime.Parse(l.Text).ToShortDateString();
                        }
                        l.Visible=true;
                    }
                }
                PlaceHolderButtons.Visible = false;
                //CheckBoxRepetition.Enabled = false;
                CheckBoxRepetition.Visible = false;
                LabelRepetition.Visible = false;
                
            }

            if (!IsPostBack)
            {
                CheckEndDate();
            }
        }

        private void CheckEndDate()
        {
            if (RadDatePickerStartDate.SelectedDate != null && RadDatePickerEndDate.SelectedDate != null)
            {
                DateTime start = ((DateTime)RadDatePickerStartDate.SelectedDate);
                DateTime end = ((DateTime)RadDatePickerEndDate.SelectedDate);

                if (end.AddMonths(-1) == start)
                {
                    RadButtonMonth.BackColor = Color.LightGreen;
                }

                //TimeSpan ts = ((DateTime)RadDatePickerEndDate.SelectedDate).Subtract(((DateTime)RadDatePickerStartDate.SelectedDate));
                //switch (ts)
                //{
                //    case TimeSpan.
                //}
            }
        }

        protected void RadButtonMonth_Click(object sender, EventArgs e)
        {
            if (RadDatePickerStartDate.SelectedDate != null)
            {
                RadDatePickerEndDate.SelectedDate = RadDatePickerStartDate.SelectedDate.Value.AddMonths(1);
                ChangeButtonColor("month");
            }
        }

        private void ChangeButtonColor(string interval)
        {
            switch (interval)
            {
                case "month": RadButtonMonth.BackColor = Color.LightGreen; 
                              RadButtonQuarter.BackColor = Color.Transparent;
                              RadButtonHalfYear.BackColor = Color.Transparent;
                              RadButtonYear.BackColor = Color.Transparent;
                              break;
                case "quarter": RadButtonMonth.BackColor = Color.Transparent;
                              RadButtonQuarter.BackColor = Color.LightGreen;
                                RadButtonHalfYear.BackColor = Color.Transparent;
                                RadButtonYear.BackColor = Color.Transparent;
                                break;
                case "halfyear": RadButtonMonth.BackColor = Color.Transparent;
                                 RadButtonQuarter.BackColor = Color.Transparent;
                                 RadButtonHalfYear.BackColor = Color.LightGreen;
                                 RadButtonYear.BackColor = Color.Transparent;
                                 break;
                case "year": RadButtonMonth.BackColor = Color.Transparent;
                             RadButtonQuarter.BackColor = Color.Transparent;
                             RadButtonHalfYear.BackColor = Color.Transparent;
                             RadButtonYear.BackColor = Color.LightGreen;
                             break;
                default: RadButtonMonth.BackColor = Color.Transparent;
                         RadButtonQuarter.BackColor = Color.Transparent;
                         RadButtonHalfYear.BackColor = Color.Transparent;
                         RadButtonYear.BackColor = Color.Transparent;
                         break; 
            }
        }

        protected void RadButtonQuarter_Click(object sender, EventArgs e)
        {
            if (RadDatePickerStartDate.SelectedDate != null)
            {
                RadDatePickerEndDate.SelectedDate = RadDatePickerStartDate.SelectedDate.Value.AddMonths(3);
                ChangeButtonColor("quarter");
            }
        }

        protected void RadButtonHalfYear_Click(object sender, EventArgs e)
        {
            if (RadDatePickerStartDate.SelectedDate != null)
            {
                RadDatePickerEndDate.SelectedDate = RadDatePickerStartDate.SelectedDate.Value.AddMonths(6);
                ChangeButtonColor("halfyear");
            }
        }

        protected void RadButtonYear_Click(object sender, EventArgs e)
        {
            if (RadDatePickerStartDate.SelectedDate != null)
            {
                RadDatePickerEndDate.SelectedDate = RadDatePickerStartDate.SelectedDate.Value.AddYears(1);
                ChangeButtonColor("year");
            }
        }

        protected void RadDatePickerStartDate_SelectedDateChanged(object sender, Telerik.Web.UI.Calendar.SelectedDateChangedEventArgs e)
        {
            CheckEndDate();
        }
    }
}